import ArgumentType from './extension-support/argument-type';
import BlockType from './extension-support/block-type';
import blockIconURI from './image/icon.svg';


class UDPTEST{
    constructor() {
        this.dgram = require('dgram');
        this.udp_client = this.dgram.createSocket('udp4'); 
        console.log("udp init");
        this.udp_ip='192.168.10.1';
        this.udp_port=8889;
    }

    setip(i,p){
        this.udp_ip=i;
        this.udp_port=p;
    }

    send(buf){
        console.log(this.udp_ip+':'+this.udp_port);
        console.log(buf);
        var SendBuff = buf;
        var SendLen = SendBuff.length;
        this.udp_client.send(SendBuff, 0, SendLen, this.udp_port, this.udp_ip); 
        
    }

}


class YourExtension{


    constructor(runtime) {
        this.runtime = runtime;
        this.udptest=new UDPTEST();
        this.udptest.send('hello')
    }

    getInfo () {
        return {
            name: 'Extension Test',
            blockIconURI: blockIconURI,
            blocks: [
                {
                    opcode: 'setip',
                    blockType: BlockType.COMMAND,
                    text: '设置ip[IP] 端口[PORT] ',
                    arguments: {
                        IP: {
                            type: ArgumentType.STRING,
                            defaultValue: "192.168.10.1"
                        },
                        PORT: {
                            type: ArgumentType.NUMBER,
                            defaultValue: 8889
                        }
                    }

                },
                {
                    opcode: 'send',
                    blockType: BlockType.COMMAND,
                    text: '发送消息[MSG] ',
                    arguments: {
                        MSG: {
                            type: ArgumentType.STRING,
                            defaultValue: "command"
                        }
                    }

                }
            ]
        }
    }
    // block运行函数, 与opcode同名
    setip (args) {
        this.udptest.setip(args.IP,args.PORT);
    }

    send (args) {
        
        this.udptest.send(args.MSG);
        
        
    }

}
module.exports = YourExtension;